#include <math.h>
#include <iostream>

double get_total_product( double[] );
double get_total_product( double[], double[] );
double get_cosine_similarity( double[], double[] );

double get_total_product_diff_avg( double[] );
double get_total_product_diff_avg( double[], double[] );
double get_Correlation_coefficient( double[], double[] );


using namespace std;

int main() {
   double a[] = {2, 3, 3, 2, 2, 3, 4};
   double a2[] = {2, 4, 3, 2, 3, 3, 4};
   double b[] = {5, 3, 1, 2, 4, 3, 1};

   cout << "a-b : \t" << get_cosine_similarity(a, b) << endl;
   cout << "a-a2 : \t" << get_cosine_similarity(a, a2) << endl;
   cout << "a-b : \t" << get_Correlation_coefficient(a, b) << endl;
   cout << "a-a2 : \t" << get_Correlation_coefficient(a, a2) << endl;
   return 0;
}

double get_total_product(double x[])
{
   return get_total_product(x, x);
}
double get_total_product(double x[], double y[])
{
   double total = 0.0;
   for (int i = 0; i < 7; ++i)
   {
      total += x[i] * y[i];
   }
   return total;
}
double get_cosine_similarity( double x[], double y[] )
{
   return get_total_product(x, y) / sqrt( get_total_product(x) * get_total_product(y) );
}


double get_Correlation_coefficient( double x[], double y[] ){
   return get_total_product_diff_avg(x , y) / sqrt (get_total_product_diff_avg(x) * get_total_product_diff_avg(y) );
}

double get_total_product_diff_avg(double x[]){
   return get_total_product_diff_avg(x, x);
}

double get_total_product_diff_avg(double x[], double y[]){
   double total = 0.0;
   
   double avg_x = 0.0;
   double avg_y = 0.0;
   for (int i = 0; i < 7; ++i)
   {
      avg_x += x[i];
      avg_y += y[i];
   }
   avg_x = avg_x/7; avg_y = avg_y/7;

   for (int i = 0; i < 7; ++i)
   {
      total += ( x[i] - avg_x ) * ( y[i] - avg_y );
   }
   return total;
}